package com.card.game;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.card.game.event.EventType;
import com.google.common.collect.Lists;

/**   
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author siyunlong  
 * @date 2016年8月24日 上午10:40:51 
 * @version V1.0   
 */
public class PlayerEvent implements Comparable<PlayerEvent>{
	private static final AtomicInteger ROOMIDSEQUENCE = new AtomicInteger(100);

	private transient int curIndex;
	private transient int tableIndex;
	private transient String userId;
	private int id;
	private boolean isCanHu;
	private boolean isCanFangGang;
	private boolean isCanDui;
	private boolean isCanAnGang;
	private boolean isCanGuoluGang;
	private boolean isCanQiangGang;
	private boolean isCanFengGang;
	private boolean isZimo;
	private List<Card> outTingCards;
	private boolean isSanJin;//新加三金倒
	
	private List<CardHeap> chiList;
	//新加的杠列表
	private List<Card> gangList = Lists.newArrayList();
	
	private transient EventType gangType = EventType.GuoLuGang;
	
	//出牌触发事件
	public PlayerEvent(String userId,int curIndex , int tableIndex ,boolean isCanHu, boolean isCanFangGang, boolean isCanDui,List<CardHeap> chiList) {
		this.id = ROOMIDSEQUENCE.incrementAndGet();
		this.userId = userId;
		this.curIndex = curIndex;
		this.tableIndex = tableIndex;
		this.isCanHu = isCanHu;
		this.isCanFangGang = isCanFangGang;
		this.isCanDui = isCanDui;
		this.chiList = chiList;
	}
	//起牌触发时间
	public PlayerEvent(String userId,int curIndex ,int tableIndex ,boolean isCanAnGang, boolean isCanGuoluGang, boolean isCanFengGang) {
		this.userId = userId;
		this.curIndex = curIndex;
		this.tableIndex = tableIndex;
		this.isCanAnGang = isCanAnGang;
		this.isCanGuoluGang = isCanGuoluGang;
		this.isCanFengGang = isCanFengGang;
	}
	//抢杠
	public PlayerEvent(String userId,int curIndex ,int tableIndex ,boolean isCanQiangGang) {
		this.userId = userId;
		this.curIndex = curIndex;
		this.tableIndex = tableIndex;
		this.isCanQiangGang = isCanQiangGang;
	}
	public PlayerEvent(String userId,int curIndex ,int tableIndex) {
		this.userId = userId;
		this.curIndex = curIndex;
		this.tableIndex = tableIndex;
	}
	public boolean isCanAnGang() {
		return isCanAnGang;
	}
	public boolean isCanHu() {
		return isCanHu;
	}

	@Override
	public int compareTo(PlayerEvent o) {
		int v1 = getValue();
		int v2 = o.getValue();
		if(v1 == v2) {
			int t1 = this.tableIndex;
			int t2 = o.tableIndex;
			if(t1>curIndex && t2>curIndex) {
				return t1-t2;
			}
			if(t1<curIndex && t2<curIndex) {
				return t1-t2;
			}
			if(t1>curIndex) return -1;
			return 1;
		}
		return v2 - v1;
	}
	
	public void setZimo(boolean isZimo) {
		this.isZimo = isZimo;
	}
	
	public boolean isFengGang() {
		return isCanFengGang;
	}
	
	public int getValue() {
		if(isCanHu) return 10;
		if(isCanFangGang) return 5;
		if(isCanFengGang) return 4;
		if(isCanDui) return 2;
		if(chiList != null && !chiList.isEmpty()) return 1;
		return 0;
	}
	
	public String getUserId() {
		return userId;
	}
	public boolean isCanQiangGang() {
		return isCanQiangGang;
	}

	public boolean isZimo() {
		return isZimo;
	}
	public void setOutTingCards(List<Card> outTingCards) {
		this.outTingCards = outTingCards;
	}
	public boolean isCanTing() {
		return outTingCards != null && !outTingCards.isEmpty();
	}
	public boolean isCanGuoluGang() {
		return isCanGuoluGang;
	}
	public void setGangList(List<Card> gangList) {
		this.gangList = gangList;
	}
	public List<Card> getGangList() {
		return gangList;
	}
	public boolean isCanChi() {
		return chiList != null && !chiList.isEmpty();
	}
	public List<Card> getChiCards(int index) {
		if(chiList == null || chiList.isEmpty()) {
			return Lists.newArrayList();
		}
		if(chiList.size() == 1) {
			return chiList.get(0).getCards();
		}
		return chiList.get(index).getCards();
	}
	public void setChiList(List<CardHeap> chiList) {
		this.chiList = chiList;
	}
	
	public EventType getGangType() {
		return gangType;
	}
	public void setGangType(EventType gangType) {
		this.gangType = gangType;
	}
	public boolean isSanJin() {
		return isSanJin;
	}
	public void setSanJin(boolean isSanJin) {
		this.isSanJin = isSanJin;
	}
	
	public PlayerEvent getOtherEvent() {
		if(!gangList.isEmpty() || isCanDui || chiList != null) {
			PlayerEvent tempEvent = new PlayerEvent(userId, curIndex, tableIndex);
			tempEvent.isCanHu = true;
			this.isCanHu = false;
			return tempEvent;
		}
		return null;
	}
	
	@Override
	public String toString() {
		return "PlayerEvent [userId=" + userId + ", curIndex=" + curIndex + ", tableIndex=" + tableIndex + ", isCanHu="
				+ isCanHu + ", isCanFangGang=" + isCanFangGang + ", isCanDui=" + isCanDui + "]";
	}
	
	public static void main(String[] args) {
		PlayerEvent e1 = new PlayerEvent("1", 1, 1);
		e1.isCanHu = true;
		PlayerEvent e2 = new PlayerEvent("2", 1, 1);
		e2.isCanDui = true;
		List<PlayerEvent> list = Lists.newArrayList(e1,e2);
		Collections.sort(list);
		System.err.println(list);
	}
}
